package com.wisely.ch8_6_1.repository.person;

import com.wisely.ch8_6_1.domain.Person;
import com.wisely.ch8_6_1.repository.IPersonDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class PersonDaoImpl implements IPersonDao {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;


    @Override
    public Person findByName(String name) {
        return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Person.class, "person");
    }

    @Override
    public List<Person> withQueryFindByAge(Integer age) {
        return mongoTemplate.find(new Query(Criteria.where("age").is(age)), Person.class, "person");
    }

    @Override
    public void save(Person person) {
        mongoTemplate.save(person,"person");
    }

    public void valOpsStr(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    public String getString(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void valOps(Object key, Object value) {
        redisTemplate.opsForValue().set(key,value);
    }

    public Object getObject(Object key) {
        return redisTemplate.opsForValue().get(key);
    }
}
